package com.css.dzj.dzjzhfx.modules.object;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjzhfx.common.audit.service.SysAuditService;
import com.css.dzj.dzjzhfx.common.util.Constant;
import com.css.dzj.dzjzhfx.common.util.HttpClientUtil;
import com.css.dzj.dzjzhfx.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/getobject")
@PropertySource(value = "classpath:config.properties")
public class GetObject {

    @Value("${apURL}")
    private String apURL;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;
    @GetMapping("/getmap")
    public R getMap(){
        String s = "";
        try{
        /**
         * 获取建设单位
         */
        String msgCon= HttpClientUtil.doGet(apURL+"/constr/countconstr");
        JSONObject jsonCon= JSON.parseObject(msgCon);
        Object constr=jsonCon.get("count");
        Map<String,Object> mapCon= JSONObject.parseObject(constr.toString());
        /**
         * 获取安评单位
         */
        String msgUnit= HttpClientUtil.doGet(apURL+"/evalunit/countunit");
        JSONObject jsonUnit= JSON.parseObject(msgUnit);
        Object unit=jsonUnit.get("count");
        Map<String,Object> mapUnit= JSONObject.parseObject(unit.toString());


        List<Map<String,Object>> list=new ArrayList();
        list.add(mapCon);
        list.add(mapUnit);
        Map<String,Object> mapAll=new HashMap<>();
        for(Map<String,Object> map:list){
            for(Map.Entry<String,Object> entry:map.entrySet()){
                String key=entry.getKey();
                Integer value=(Integer) entry.getValue();
                Integer valueAll=(Integer) mapAll.get(entry.getKey());
                if(valueAll==null){
                    mapAll.put(key,value);
                }else{
                    valueAll=valueAll.intValue()+value.intValue();
                    mapAll.put(key,valueAll);
                }
            }
        }
        List<Map<String,Object>> listNew=new ArrayList();
        Integer num=0;
        for (Map.Entry<String,Object> entry:mapAll.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            String newName=name.replace("市","");
            Integer value=(Integer)entry.getValue();
            num+=value;
            map.put("name",newName);
            map.put("value",value);
            listNew.add(map);
        }
            String dataUnit=JSON.toJSONString(listNew);
            sysAuditService.saveSuccessAudit("查询成功", Constant.GETOBJECT, Constant.PT, Constant.ZHFX_SELECT, dataUnit, request);
            return R.ok().put("dataUnit",dataUnit).put("cityTotal",num);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.GETOBJECT, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 获取饼状图
     * @return
     */
    @GetMapping("/getpie")
    public R getPie(){
        String s = "";
        try {
            /**
             * 获取建设单位
             */
            String msgCon = HttpClientUtil.doGet(apURL + "/constr/countconstr");
            JSONObject jsonCon = JSON.parseObject(msgCon);
            Object constr = jsonCon.get("count");
            Map<String, Object> mapCon = JSONObject.parseObject(constr.toString());
            List listContr = new ArrayList();
            for (Map.Entry<String, Object> entry : mapCon.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listContr.add(map);
            }
            String dataConstr = JSON.toJSONString(listContr);
            /**
             * 获取安评单位
             */
            String msgUnit = HttpClientUtil.doGet(apURL + "/evalunit/countunit");
            JSONObject jsonUnit = JSON.parseObject(msgUnit);
            Object unit = jsonUnit.get("count");
            Map<String, Object> mapUnit = JSONObject.parseObject(unit.toString());
            List listUnit = new ArrayList();
            for (Map.Entry<String, Object> entry : mapUnit.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listUnit.add(map);
            }
            String dataUnit = JSON.toJSONString(listUnit);
            sysAuditService.saveSuccessAudit("查询成功", Constant.GETOBJECT, Constant.PT, Constant.ZHFX_SELECT, dataUnit, request);
            return R.ok().put("dataUnit", dataUnit).put("dataConstr", dataConstr);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.GETOBJECT, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 获取柱状图
     * @return
     */
    @GetMapping("/getbar")
    public R getBar(){
        String s = "";
        try{
        /**
         * 获取建设单位
         */
        String msgCon= HttpClientUtil.doGet(apURL+"/constr/countconstr");
        JSONObject jsonCon= JSON.parseObject(msgCon);
        Object constr=jsonCon.get("count");
        Map<String,Object> mapCon= JSONObject.parseObject(constr.toString());
        List listContr=new ArrayList();

        /**
         * 获取安评单位
         */
        String msgUnit= HttpClientUtil.doGet(apURL+"/evalunit/countunit");
        JSONObject jsonUnit= JSON.parseObject(msgUnit);
        Object unit=jsonUnit.get("count");
        Map<String,Object> mapUnit= JSONObject.parseObject(unit.toString());
        List listUnit=new ArrayList();
        /**
         * 获取建设单位地址
         */
        List<String> listCon=new ArrayList<>();
        for (Map.Entry<String,Object> entry:mapCon.entrySet()) {
            String name=entry.getKey();
            listCon.add(name);
        }
        /***
         * 获取安评单位地址
         */
        List<String> listUni=new ArrayList<>();
        for (Map.Entry<String,Object> entry:mapUnit.entrySet()) {
            String name=entry.getKey();
            listUni.add(name);
        }
        for (String con:listCon) {
            if(!listUni.contains(con)){
                mapUnit.put(con,0);
            }
        }
        for (String uni:listUni){
            if (!listCon.contains(uni)){
                mapCon.put(uni,0);
            }
        }
        for (Map.Entry<String,Object> entry:mapCon.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listContr.add(map);
        }
        String dataConstr=JSON.toJSONString(listContr);

        for (Map.Entry<String,Object> entry:mapUnit.entrySet()) {
            HashMap map=new HashMap();
            String name=entry.getKey();
            Integer value=(Integer)entry.getValue();
            map.put("name",name);
            map.put("value",value);
            listUnit.add(map);
        }
            String dataUnit=JSON.toJSONString(listUnit);
            sysAuditService.saveSuccessAudit("查询成功", Constant.GETOBJECT, Constant.PT, Constant.ZHFX_SELECT, dataUnit, request);
            return R.ok().put("dataUnit",dataUnit).put("dataConstr",dataConstr);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.GETOBJECT, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
